package cn.relx.org.service.impl;

import cn.relx.basic.config.BussinessException;
import cn.relx.basic.util.BaiduAuditUtils;
import cn.relx.basic.util.EmailUtil;
import cn.relx.org.domain.Employee;
import cn.relx.org.domain.Shop;
import cn.relx.org.domain.ShopAuditLog;
import cn.relx.org.mapper.EmployeeMapper;
import cn.relx.org.mapper.ShopAuditLogMapper;
import cn.relx.org.mapper.ShopMapper;
import cn.relx.org.service.IShopService;
import cn.relx.basic.service.impl.BaseServiceImpl;
import cn.relx.org.vo.ShopVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author relx
 * @since 2022-10-15
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;


    @Override
    @Transactional
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        Employee employee = this.verify(shop, admin);
        // 存入数据了
        // 添加shop表
        shopMapper.add(shop);
        if(null == employee){
            //添加emp
            employee = admin;
            employeeMapper.add(employee);
        }
        employee.setShopId(shop.getId());
        employeeMapper.update(employee);
        Shop uptShop = new Shop();
        uptShop.setId(shop.getId());
        uptShop.setAdminId(employee.getId());
        shopMapper.update(uptShop);
    }

    @Override
    @Transactional
    public Map<String, Object> shopEcharsData() {
        List<ShopVo> shopVo = shopMapper.shopEcharsData();
        List<Integer> x = shopVo.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> y = shopVo.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        Map<String,Object> data = new HashMap<>();
        data.put("x",x);
        data.put("y",y);
        return data;
    }

    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) {
        shopAuditLog.setAuditId(324L);
        shopAuditLogMapper.add(shopAuditLog);
        EmailUtil.Email("a932984913@vip.qq.com","你好，你的店铺已审核通过，请点击激活链接进行店铺激活"
                + "激活后方可正常使用!"
                + "激活链接:http://localhost:80/shop/audit/"+shopAuditLog.getShopId(),"");
//        //发送激活右键给用户，此处各位使用定义好的发送右键工具类进行发送
//        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//        //设置发送人
//        simpleMailMessage.setFrom("a932984913@vip.qq.com");
//        //设置接收人，此处应根据店铺信息查询管理员邮箱，自行实现
//        simpleMailMessage.setTo("a932984913@vip.qq.com");
//        //设置邮件主题
//        simpleMailMessage.setSubject("店铺审核结果通知");
//        //设置右键文本内容
//        simpleMailMessage.setText("你好，你的店铺已审核通过，请点击激活链接进行店铺激活"
//                + "激活后方可正常使用!"
//                + "激活链接:http://localhost:80/shop/audit/"+shopAuditLog.getShopId());
//        //使用右键核心对象发送邮件
//        javaMailSender.send(simpleMailMessage);
    }

    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) {
        shopAuditLog.setAuditId(334L);
        shopAuditLogMapper.add(shopAuditLog);
        Shop shop = new Shop();
        shop.setState(-1);
        shop.setId(shopAuditLog.getShopId());
        shopMapper.update(shop);
        EmailUtil.Email("a932984913@vip.qq.com","你好，你的店铺审核未通过，请修改信息后重新提交!"
                + "失败原因:'"+shopAuditLog.getNote()+"'"
                + "修改链接:http://localhost:8081/#/RegisterAgain?shopId="+shopAuditLog.getShopId(),"");
//        //发送激活右键给用户，此处各位使用定义好的发送右键工具类进行发送
//        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//        //设置发送人
//        simpleMailMessage.setFrom("a932984913@vip.qq.com");
//        //设置接收人，此处应根据店铺信息查询管理员邮箱，自行实现
//        simpleMailMessage.setTo("a932984913@vip.qq.com");
//        //设置邮件主题
//        simpleMailMessage.setSubject("店铺审核结果通知");
//        //设置右键文本内容
//        simpleMailMessage.setText("你好，你的店铺审核未通过，请修改信息后重新提交!"
//                + "失败原因:'"+shopAuditLog.getNote()+"'"
//                + "修改链接:http://localhost:8081/#/RegisterAgain?shopId="+shopAuditLog.getShopId());
//        //使用右键核心对象发送邮件

//        javaMailSender.send(simpleMailMessage);
    }

    @Override
    @Transactional
    public void updateByIdAudit(Long id) {
        shopMapper.updateByIdAudit(id);
    }

    @Override
    @Transactional
    public void importExcel(List<Shop> shops) {
        shopMapper.importExcel(shops);
    }

    private Employee verify(Shop shop, Employee admin) {
        //检验参数
        if(StringUtils.isBlank(shop.getName())){
            throw new BussinessException("店铺名不能为空!");
        }
        //检验两次密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BussinessException("两次密码不一致!");
        }
        //AI检验
        if(!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("店铺LoGo不合规!");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称不合规!");
        }
        //店铺名称是否存在
        Shop tmpShop = shopMapper.findByName(shop.getName());
        if (null != tmpShop){
            throw new BussinessException("店铺名称已存在!");
        }
        // 当前管理员是否管理了其他店铺
        Employee employee = employeeMapper.findByName(admin.getUsername());
        if(null != employee && null != employee.getShopId()){
            Shop shopById = shopMapper.findById(employee.getShopId());
                if(shopById.getAdminId().equals(employee.getId())){
                    throw new BussinessException("管理员已关联店铺!");
                }
        }
        return employee;
    }
}
